/*
 * 文件名：FreeCarServiceImpl.java	 
 * 时     间：下午6:27:38
 * 作     者：lyf      
 * 版     权： 2012-2022 湖南智软网络科技, 公司保留所有权利.
 * 联     系：http://www.073586.com/
 */
package com.local.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cz073586.common.billRules.utils.DateUtil;
import com.cz073586.tools.util.Tools;
import com.local.common.CommonContains;
import com.local.dao.mapers.FreeCarMapper;
import com.local.dao.mapers.LocalCarInRepertoryMapper;
import com.local.model.FreeCar;
import com.local.model.FreeCarExample;
import com.local.model.LocalCarInRepertory;
import com.local.model.LocalCarInRepertoryExample;
import com.local.page.FreeCarPage;
import com.local.service.face.FreeCarService;
import com.qianhai.common.dao.face.BaseMapper;
import com.qianhai.common.exception.BaseException;
import com.qianhai.common.service.impl.BaseServiceImpl;
import com.qianhai.common.utils.UUIDGenerateUtil;

/**
 * 类描述:这里是类描述
 * 
 * @ClassName: FreeCarServiceImpl
 * @author lyf
 * @date 2016年2月20日 下午6:27:38
 */
@Service
public class FreeCarServiceImpl extends
		BaseServiceImpl<FreeCarExample, FreeCar, String> implements
		FreeCarService
{
	
	@Autowired
	private FreeCarMapper freeCarMapper;
	
	@Autowired
	private LocalCarInRepertoryMapper localCarInRepertoryMapper;
	
	@Override
	public Integer addFreeCar(FreeCar freecar) throws BaseException
	{
		try
		{
			if (Tools.isEmpty(freecar.getCarNo()))
			{
				throw new BaseException("50000-00001-00039");
			}
			FreeCar fcar=this.queryFreeCarNo(freecar.getCarNo());
			if(!Tools.isEmpty(freecar.getUuid())){
				fcar=freeCarMapper.selectByPrimaryKey(freecar.getUuid());
				if(!freeCarMapper.selectByPrimaryKey(freecar.getUuid()).getCarNo().equalsIgnoreCase(freecar.getCarNo())){
					if(!Tools.isEmpty(this.queryFreeCarNo(freecar.getCarNo()))){
						throw new BaseException("15000-00000-00021"); 
					}
				}
			}
			/*if(!Tools.isEmpty(fcar)){
				throw new BaseException("15000-00000-00021");
			}*/
			if (Tools.isEmpty(freecar.getStatus()))
			{
				freecar.setStatus(1);
			}
			if (!Tools.isEmpty(fcar))
			{
				freecar.setUuid(fcar.getUuid());
				freeCarMapper.updateByPrimaryKeySelective(freecar);
				return 1;
			}

			freecar.setUuid(UUIDGenerateUtil.generate());
			
			if (Tools.isEmpty(freecar.getTimeInterval()))
			{
				Date intervalTime = DateUtil
						.getDateByString("2030-12-31 23:59:59");
				freecar.setTimeInterval(intervalTime);
			}
			
			if (Tools.isEmpty(freecar.getStatus()))
			{
				freecar.setStatus(1);
			}
			
			freeCarMapper.insertSelective(freecar);
			LocalCarInRepertoryExample carInEx = new LocalCarInRepertoryExample();
			carInEx.or().andCarNoEqualTo(freecar.getCarNo().trim());
			List<LocalCarInRepertory> localCarInRepertories = localCarInRepertoryMapper.selectByCondition(carInEx);
			if (!Tools.isEmpty(localCarInRepertories)) {
			for (LocalCarInRepertory carInRepertory : localCarInRepertories) {
				if(!Tools.isEmpty(carInRepertory)){
					carInRepertory.setExpireTime(freecar.getTimeInterval());
					carInRepertory.setOpenType(CommonContains.OPEN_TYPE_TRUE);
					carInRepertory.setPayType(CommonContains.FREE_LIST);
					localCarInRepertoryMapper.updateByPrimaryKeySelective(carInRepertory);
				}
			}
			}
			
		} catch (Throwable t)
		{
			log.error(t.getMessage(), t);
			throw new BaseException(t.getMessage());
		}
		return null;
	}
	
	@Override
	public BaseMapper<FreeCarExample, FreeCar, String> getMapper()
	{
		
		return freeCarMapper;
	}

	@Override
	public FreeCar queryFreeCarNo(String carNo) throws BaseException
	{
		// TODO Auto-generated method stub
		FreeCarExample condition = new FreeCarExample();
		condition.or().andCarNoEqualTo(carNo);
		List<FreeCar> list = freeCarMapper.selectByCondition(condition);
		return list.isEmpty() ? null : list.get(0);
	}

	@Override
	public List<FreeCar> queryFreeCarQueryForm(FreeCarPage<FreeCar> pageInfo)
	{
		// TODO Auto-generated method stub
		Integer recordCount = freeCarMapper.countAllByPage(pageInfo);
		pageInfo.setTotalRecords(recordCount);
		List<FreeCar> list = freeCarMapper.queryByPage(pageInfo);
		Date curTime = new Date();
		for (FreeCar freeCar : list) {
			if(freeCar.getTimeInterval().getTime()< curTime.getTime()){
				freeCar.setStatus(0);
			}
		}
		pageInfo.setData(list);
		return list;
	}
	
}
